package io.xxx.xbutler.core.get;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.client.StoreClient;
import io.xxx.xbutler.domain.task.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 替换门店首页
 */
@Slf4j
@Component
public class StoreGetter implements ContentGetter {

    @Resource
    private StoreClient storeClient;

    @Value("${mini-program.url.thumb.home:https://front-xps-cdn.xsyx.xyz/custom/leo/1740033832080.png}")
    private String homeThumbUrl;

    @Override
    public void replaceContent(PushContext context) {
        Receiver receiver = context.getReceiver();
        Long storeId = receiver.getStoreId();
        if (storeId == null) {
            return;
        }

        List<TaskItem> taskItems = context.getTaskItems();
        for (TaskItem taskItem : taskItems) {
            if (taskItem.getContentType() == null) {
                continue;
            }
            if (!taskItem.assign(MessageType.MINI_PROGRAM, ContentType.HOME)) {
                continue;
            }

            JSONObject content = taskItem.getContent();
            if (storeClient.match(storeId, content.getLong("labelItemId"))) {
                content.put("thumbUrl", homeThumbUrl);
            }
        }
    }
}
